import sensor
import image
import lcd
import time
lcd.init()
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.set_auto_gain(0)#关闭自动增益
sensor.set_hmirror(1)
sensor.set_vflip(1)
sensor.run(1)
white_threshold =(82, 96, -11, 20, -10, 10)
green_threshold   =(16, 80, -7, 11, -9, 13)#阈值 黑
red_threshold =(82, 93, 6, 35, -9, 31)#
white_coordinate=[0,0]
read_coordinate=[0,0]
green__coordinate=[0,0]

#def find_max(blobs):#寻找最大色块
    #max_size=0
    #for blob in blobs:
        #if blob[2]*blob[3] > max_size:
            #max_blob=blob
            #max_size = blob[2]*blob[3]
    #return max_blob
def find_max(blobs):
    max_blob = None
    max_size = 0
    for blob in blobs:
        if blob[2] * blob[3] > max_size:
            max_blob = blob
            max_size = blob[2] * blob[3]
    return max_blob

while True:
    img=sensor.snapshot()
    try:
        blobs = img.find_blobs([green_threshold])
        maxb_index=find_max(blobs)
        b=find_max(blobs)
        tmp=img.draw_rectangle(b[0:4])#画框
        tmp=img.draw_cross(b[5], b[6])#画点
        green_coordinate=[b[5], b[6]]
        print("green",green_coordinate)
        #c=img.get_pixel(b[5], b[6])
        #print(c)
        #print(blobs)
    except:
        pass


    lcd.display(img)
